package com.xilihui.xlh.business.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.ChecklistActivity;
import com.xilihui.xlh.business.activitys.TakeGoodsActivity;
import com.xilihui.xlh.business.adapters.BasePagerAdapter;
import com.xilihui.xlh.business.dialogs.BuyDialog;
import com.xilihui.xlh.business.dialogs.MarqueDialog;
import com.xilihui.xlh.business.entities.GoodDetailsEntity;
import com.xilihui.xlh.business.entities.TakeGoodsEntity;
import com.xilihui.xlh.business.requests.MyAppointmentRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.recyclerview.LinearLayoutColorDivider;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatFragment;
import com.xilihui.xlh.core.exception.DefaultExceptionListener;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/29.
 */

public class TakeGoodsFragment extends BaseCompatFragment implements OnRefreshListener, OnLoadMoreListener, View.OnClickListener {
    
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    ExceptionManager exceptionManager;
    
    BaseAdapter<TakeGoodsEntity.GoodsBean> baseAdapter;
    ArrayList<TakeGoodsEntity.GoodsBean> goods = new ArrayList<>();
    
    int page = 1;
    String id = "";
    String cate_id = "";
    String wa_id = "";
    
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_take_goods,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        exceptionManager = new ExceptionManager(smartRefreshLayout,new DefaultExceptionListener(this)){
            @Override
            protected int getEmptyView() {
                return R.layout.empty_goods;
            }
        };
        
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        
        baseAdapter = new BaseAdapter<TakeGoodsEntity.GoodsBean>(getActivity(),goods) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_take_goods;
            }
                
            @Override
            protected void getView(BaseViewHolder holder, final TakeGoodsEntity.GoodsBean entity, int position) {

                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.iv_img);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+entity.getOriginal_img());
                holder.setText(R.id.tv_shopName,entity.getGoods_name());
                holder.setText(R.id.tv_price,"￥"+entity.getShop_price());
                
                
                holder.setOnClickListener(R.id.tv_take, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        
                        MyAppointmentRequest.goodDetails(getActivity(),entity.getGoods_id())
                                .compose(DoTransform.<GoodDetailsEntity>applyScheduler(getActivity(),true))
                                .subscribe(new HttpSubscriber<GoodDetailsEntity>(getActivity()) {
                                    @Override
                                    protected void onSuccess(GoodDetailsEntity entity) {
                                        BuyDialog buyDialog = new BuyDialog(getActivity(),entity,id,wa_id);
                                        buyDialog.show();
                                    }

                                    @Override
                                    protected void onFail(String msg) {
                                        ToastUtil.toastShortNegative(msg);
                                    }
                                });
                        
                    }
                });
            }
        };
        
        recyclerView.addItemDecoration(new LinearLayoutColorDivider(getResources(),R.color.view_line,R.dimen._5dp, LinearLayout.VERTICAL));
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(baseAdapter);
        baseAdapter.setAnimationsLocked(true);
        getExtras();
        getData(true);
        
        
    }

    private void getExtras() {
        id = getArguments().getString("id","");
        cate_id = getArguments().getString("cate_id", "");
        wa_id = getArguments().getString("wa_id","");
    }


    public void getData(boolean isLoading){
        MyAppointmentRequest.takeGoods(getActivity(),id,cate_id,page,wa_id)
                .compose(DoTransform.<TakeGoodsEntity>applyScheduler(getActivity(),isLoading))
                .compose(this.<TakeGoodsEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<TakeGoodsEntity>(getActivity()) {
                    @Override
                    protected void onSuccess(TakeGoodsEntity entity) {
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                        if(page==1){
                            goods.clear();
                        }
                        goods.addAll(entity.getGoods());
                        baseAdapter.setList(goods);                       
                        
                        if(goods.size()>0){
                            exceptionManager.hide();
                        }else {
                            exceptionManager.showEmpty();
                        }
                        
                        if(page>entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
        

    }


    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page +1;
        getData(false);
    }

    @Override
    public void onClick(View view) {
        
    }
}
